﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PhoneCore.Framework;
using PhoneCore.Framework.Bootstrap;
using PhoneCore.Framework.Configuration;
using PhoneCore.Framework.IoC;
using PhoneCore.Framework.IoC.LifetimeManagers;
using PhoneCore.Framework.Navigation;
using PhoneCore.Framework.Views;
using SecureBox.UI.Infrastructure.Data;
using SecureBox.UI.ViewModel;

namespace SecureBox.UI.UnitTests.ViewModel
{
    [TestClass]
    public class AccountViewPageModelTests
    {
        [TestMethod]
        public void CanSave()
        {
            using (IContainer container = TestHelper.GetContainer())
            {
                container.RegisterType(typeof(IBootstrapperService), typeof(BootstrapperService),
                                   ConfigSettings.Instance.GetSection("system/bootstrapping"));
                IBootstrapperService service = container.Resolve<IBootstrapperService>();
                service.Run();
                AccountViewPageModel viewModel = container.Resolve<IPageMapping>()
                    .GetViewModel("Account") as AccountViewPageModel;
                viewModel.AccountName = "TestAccount";
                viewModel.Email = "email@test.com";
                viewModel.Keyword = "test";
                viewModel.Password = "my password";
                viewModel.Name = "test name";

                //act
                viewModel.OnSaveCommand();

                using (IDataContextService dataContextService = container.Resolve<IDataContextService>())
                {
                    //Assert
                    var record = dataContextService.Records.Get(r => r.Name == "test name");
                    Assert.AreEqual("Account", record.Template.Name);
                    Assert.AreEqual("test name", record.Name);
                    Assert.AreEqual("email@test.com", record.Properties.Single(p => p.Name == "Email").Value);
                    Assert.AreEqual("my password", record.Properties.Single(p => p.Name == "Password").Value);
                    Assert.AreEqual("test", record.Keywords.Single(p => p.Name == "test").Name);
                    dataContextService.Delete();
                }
            }
        }
    }
}
